﻿(function($){
    $.fn.extend({ 
        playlistRandom: function(options) {           
	        var defaults = {
		        jsonData: {},
		        idInfo: {},
		        idPlayerlist: {}
	        };
	        
	        var options =  $.extend(defaults, options);
	        
	        return this.each(function() {
		        var opt = options;
		        var id = $("#" + opt.idPlayerlist);
		        var z = 0;		        		        
		        
		        for(z = 1 ; z < opt.jsonData.length; z++)
		        {
		            if("listkey-random-" + opt.jsonData[z].listkey == opt.idPlayerlist)
		            {
		                listtemp = opt.jsonData[0];
		                opt.jsonData[0] = opt.jsonData[z];
		                opt.jsonData[z] = listtemp;
		                break;
		            }
		        }
		        
		        // Dom PlayerList Info
		        var html = "";
		        html += "<li>";
		        html += "   <h1 class=\"title\">";
		        html += "       <a title=\"" + opt.jsonData[0].listname + "\" href=\"/nghe?L=" + opt.jsonData[0].listkey + "\"><strong>" + opt.jsonData[0].listname + "</strong></a>";
		        html += "   </h1>";
		        html += "</li>";
		        html += "<li>";
		        html += "   <span class=\"grey\">Tạo bởi: </span>";
		        html += "   <a title=\"Nghe playlist của" + opt.jsonData[0].username + "\" href=\"/playlist/" + opt.jsonData[0].username + "\">" + opt.jsonData[0].username + "</a>";
		        html += "   <span class=\"grey\" > | </span>";
		        html += "   <span class=\"grey\">Số bài: " + opt.jsonData[0].countsong + "</span>";
		        html += "</li>";		        		        
		        
                $("#" + opt.idInfo).html(html);
                
                // Thực hiện Remove và Append Button Playlist
                var parent = $("#" + opt.idPlayerlist).parent();
                $("#" + opt.idPlayerlist).hide("slow",function(){
                    $(this).remove();
                    $(parent).append("<div onclick=\"FreshPlaylist('" + opt.jsonData[z].listkey + "');\" id=\"listkey-random-" + opt.jsonData[z].listkey + "\" class=\"item\" title=\"" + opt.jsonData[z].listname + "\"><h1 class=\"title\"><div style=\"overflow: hidden; width: 220px; height:40px; line-height:40px;\"><strong>" + opt.jsonData[z].listname +  "</strong></div></h1></div>");
                });
	        });
        }
    });
})(jQuery);

$(document).ready(function() { 
    $(".random-playlist .relation .item").live("click", function() {
        $(document.body).playlistRandom({
            jsonData: jsonPlaylist,
            idInfo: "summary-info",
            idPlayerlist: $(this).attr("id")
        });
    });
});